package com.mql.ganio.ui.presenter;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.mql.ganio.R;
import com.mql.ganio.api.ApiRetrofit;
import com.mql.ganio.model.response.GanIoResponse;
import com.mql.ganio.ui.GanHuoAdapter;
import com.mql.ganio.ui.activity.ViewBigImageActivity;
import com.mql.ganio.ui.base.BaseActivity;
import com.mql.ganio.ui.base.BasePresenter;
import com.mql.ganio.ui.base.baseadapter.BaseRecyclerViewAdapter;
import com.mql.ganio.ui.view.IGanHuoAtView;
import com.mql.ganio.util.UIUtils;

import java.util.ArrayList;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 作者：Administrator on 2017/11/8 12:50
 */
public class GanHuoAtPresenter extends BasePresenter<IGanHuoAtView> {
    private GanHuoAdapter adapter;
    private List<GanIoResponse.ResultsBean> list;
    ArrayList<String> imgList = new ArrayList<>();
    private BaseActivity context;

    public GanHuoAtPresenter(BaseActivity context) {
        super(context);
        list = new ArrayList<>();
        this.context = context;
    }

    private void setAdapter(final BaseActivity context, List list) {
        if (adapter == null) {
            adapter = new GanHuoAdapter(context, list);
            getView().getXRecyclerView().setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
            getView().getXRecyclerView().setAdapter(adapter);
            adapter.setOnItemClickListener(new BaseRecyclerViewAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    Bundle bundle = new Bundle();
                    bundle.putInt("selet", 2);// 2,大图显示当前页数，1,头像，不显示页数
                    bundle.putInt("code", position);//第几张
                    bundle.putStringArrayList("imageuri", imgList);
                    Intent intent = new Intent(context, ViewBigImageActivity.class);
                    intent.putExtras(bundle);
                    context.jumpToActivity(intent);

                }
            });
        } else {
            adapter.setDatas(list);
        }
        getView().getXRecyclerView().refreshComplete();
        getView().getXRecyclerView().loadMoreComplete();
        mContext.hideWaitingDialog();
    }

    public void back() {
        context.finish();
    }

    public void refreshData(final int page) {

        mContext.showWaitingDialog(UIUtils.getString(R.string.loading));
        ApiRetrofit.getInstance().getGanIoData("福利", page, 20)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<GanIoResponse>() {
            @Override
            public void call(GanIoResponse ganIoResponse) {
                if (page == 1) {
                    if (adapter != null)
                        adapter.clear();
                }
                List<GanIoResponse.ResultsBean> ganList=ganIoResponse.getResults();
                for (GanIoResponse.ResultsBean bean:ganList){
                    imgList.add(bean.getUrl());
                }
                list.addAll(ganList);
                setAdapter(context, list);
            }
        });
    }
}
